var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        mode:"lists",           // 显示列表方式

		auction_id:"",			// 活动id
        data: '',               // 总数据
        dataList: '',           
        starselect: '',         // 星级
        starno: '',
        mask: false,
        overly: false,
        goods_id:[],            // 商品id

		buyGoodsList:[],		// 起始价 或 封顶价购买时获取商品数据
    },
    // 关注
    attentionEvent(){
        var self = this;
        var url = "";
        var auctionData = this.data.data;
        var auction_id = this.data.auction_id;
        console.log(auctionData.auction.focus_status)
        if (auctionData.auction.focus_status == 0){
            url = "/focus/auction";
            auctionData.auction.focus_status = 1;
            auctionData.auction.focus_num += 1
        }else{
            url = "/focus/auctioncancel";
            auctionData.auction.focus_status = 0;
            auctionData.auction.focus_num -= 1
        }

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: auction_id
                    },
                    success: function (res) {
                        var list = res.data;
                        if(list.code == 1){
                            self.setData({
                                data: auctionData
                            })
                        }
                    }
                })
            },
        })
    },

	// 显示弹窗
	

    // 勾选与取消勾选
    changeCheckbox(e){
        var value = e.detail.value;
        this.setData({
            goods_id:value
        })
    },
	// 获取商品数据状态  弹窗
	showGoodsList(e){
		var self = this;
		var type = e.target.dataset.type;
		var url = "/index/bulkgoodslist";
		var auction_id = this.data.auction_id;
		var buyGoodsList = [];

		this.setData({ type, mask: true,goods_id:[]});

		wx.getStorage({
			key: 'user',
			success: function (res) {
				wx.request({
					url: app.url.requestUrl + url,
					method: 'POST',
					data: {
						openid: res.data.openid,
						token: res.data.token,
						id: auction_id
					},
					success: function (res) {
						var list = res.data;
						if (list.code == 1){
							self.setData({
								buyGoodsList:list.data
							})
						}
					}
				})
			},
		})
	},
	hideGoodsList(){
		this.setData({ mask: false})
	},
	// 购买提交
	submit(){
		var self = this;
		var type = this.data.type;
		var id = this.data.goods_id.join(",");
        var auction_id = this.data.auction_id;
		if (!id) {
			wx.showToast({
				image: "../../img/warning.png",
				title: '请勾选商品',
			})
			return false;
		}
		if(type == "start"){
			this.dialog2.showDialog();
		} else if (type == "end"){
			wx.navigateTo({
                url: '../group-order/group-order?id=' + id + '&auction_id=' + auction_id,
			})
		}
		
	},
    // 生命周期函数--监听页面加载
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中',
            mask: true
        })

        this.dialog1 = this.selectComponent("#dialog1");		// 关注成功弹框
        this.dialog2 = this.selectComponent("#dialog2");		// 委托出价弹框

        this.setData({
            auction_id: options.id || 149
        })

        var self = this;
        wx.getStorage({
            key: 'user',
            success: function (res) {
                console.log("success")
                self.getAuctionInfo(res, options.id || 149)
            },
            fail() {
                wx.login({
                    success: res => {
                        console.log("fail")
                        wx.request({
                            url: app.url.requestUrl + '/login/login',
                            method: 'POST',
                            data: { code: res.code },
                            success: function (data) {
                                if (data.data.code == 1) {
                                    self.getAuctionInfo(data.data, options.id || 149)
                                }
                            }
                        })
                    }
                })
            }
        })
    },
    // 获取活动数据
    getAuctionInfo(res, id) {
        var self = this;
        wx.request({
            url: app.url.requestUrl + '/index/bulkdetails',
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
                id: id
            },
            success: function (datas) {
                if (datas.data.code == 1) {
                    datas.data.data.auction.score = app.globalData.levelRuleCount(datas.data.data.auction.score)

                    self.setData({
                        auction_id:id,
                        data: datas.data.data,
                    })
                    wx.setNavigationBarTitle({ title: datas.data.data.auction.name || '一位不愿透露姓名的网友' })
                } else {
                    if (datas.data.code == -1 || datas.data.code == 404) {
                        self.loginOverdue();
                    }
                }
                wx.hideLoading();
            }
        })
    },
    // 登录过期状态
    loginOverdue(e) {
        var self = this;
        wx.removeStorage({
            key: 'user',
            success: function (res) {
                self.onLoad();
            }
        })
    },

    onShareAppMessage(options) {
        var auction = this.data.data.auction;

        if (options.form == "button") { console.log("来自按钮") }

        return {
            title: '拍卖活动：' + auction.title,
            path: `pages/group-detail/group-detail?id=${auction.id}`
        }
    },
    // 关注
    focusActivify(e) {
        this.dialog1.showDialog();
    },
    // 关注取消按钮
    __focusNoEvent(){
        this.dialog1.hideDialog();
    },
    // 关注确认按钮
    __focusYesEvent(){
        this.attentionEvent();
        this.dialog1.hideDialog();
    },
	// 起始价购买 - 确认
	__buyYesEvent(){
		var self = this;
		var id = this.data.goods_id.join(",");
		var auction_id = this.data.auction_id;
		var url = "/order/low_price_buy";

		wx.getStorage({
			key: 'user',
			success: function (res) {
				wx.request({
					url: app.url.requestUrl + url,
					method: 'POST',
					data: {
						openid: res.data.openid,
						token: res.data.token,
						auction_id: auction_id,
						goods_ids: id
					},
					success: function (res) {
						var stauts = res.data;
						if (stauts.code == 1) {
							wx.showToast({
								title: '购买成功',
							})
							self.setData({
								mask:false
							})
						}else{
							wx.showToast({
								title: stauts.msg,
							})
						}
					}
				})
			},
		})
		this.dialog2.hideDialog();
	},
	// 起始价购买 - 取消
	__buyNoEvent(){
		this.dialog2.hideDialog();
	},
    overly: function (e) {
        this.setData({
            mask: false,
            overly: false
        })
    },
})